package com.ruoyi.generator.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generator.domain.GenTemplate;
import com.ruoyi.generator.domain.GenTemplateFile;
import com.ruoyi.generator.service.IGenTemplateFileService;
import com.ruoyi.generator.service.IGenTemplateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代码模版文件Controller
 * 
 * @author aibozeng
 * @date 2023-04-17
 */
@Controller
@RequestMapping("/generator/templateFile")
public class GenTemplateFileController extends BaseController
{
    private String prefix = "generator/templateFile";

    @Autowired
    private IGenTemplateFileService genTemplateFileService;
    @Autowired
    private IGenTemplateService genTemplateService;

    @RequiresPermissions("generator:templateFile:view")
    @GetMapping()
    public String templateFile()
    {
        return prefix + "/templateFile";
    }

    /**
     * 查询代码模版文件列表
     */
    @RequiresPermissions("generator:templateFile:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GenTemplateFile genTemplateFile)
    {
        startPage();
        List<GenTemplateFile> list = genTemplateFileService.selectGenTemplateFileList(genTemplateFile);
        return getDataTable(list);
    }

    /**
     * 按模版id,进入文件列表页
     */
    @GetMapping("/listByTemplate/{templateId}")
    public String listByTemplate(@PathVariable("templateId") Long templateId, ModelMap mmap)
    {
        GenTemplate genTemplate = genTemplateService.selectGenTemplateByTemplateId(templateId);
        mmap.put("genTemplate", genTemplate);
        // 获取模版下的文件列表
        GenTemplateFile query = new GenTemplateFile();
        query.setTemplateId(templateId);
        List<GenTemplateFile> list = genTemplateFileService.selectGenTemplateFileList(query);
        mmap.put("genTemplateFiles", list);
        return prefix + "/listByTemplate";
    }
    
    /**
     * 导出代码模版文件列表
     */
    @RequiresPermissions("generator:templateFile:export")
    @Log(title = "代码模版文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GenTemplateFile genTemplateFile)
    {
        List<GenTemplateFile> list = genTemplateFileService.selectGenTemplateFileList(genTemplateFile);
        ExcelUtil<GenTemplateFile> util = new ExcelUtil<GenTemplateFile>(GenTemplateFile.class);
        return util.exportExcel(list, "代码模版文件数据");
    }

    /**
     * 新增代码模版文件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存代码模版文件
     */
    @RequiresPermissions("generator:templateFile:add")
    @Log(title = "代码模版文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GenTemplateFile genTemplateFile)
    {
        return toAjax(genTemplateFileService.insertGenTemplateFile(genTemplateFile));
    }

    /**
     * 修改代码模版文件
     */
    @RequiresPermissions("generator:templateFile:edit")
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        GenTemplateFile genTemplateFile = genTemplateFileService.selectGenTemplateFileByFileId(fileId);
        mmap.put("genTemplateFile", genTemplateFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存代码模版文件
     */
    @RequiresPermissions("generator:templateFile:edit")
    @Log(title = "代码模版文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GenTemplateFile genTemplateFile)
    {
        return toAjax(genTemplateFileService.updateGenTemplateFile(genTemplateFile));
    }

    /**
     * 删除代码模版文件
     */
    @RequiresPermissions("generator:templateFile:remove")
    @Log(title = "代码模版文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(genTemplateFileService.deleteGenTemplateFileByFileIds(ids));
    }
}
